<?php
/**
 * Created by PhpStorm.
 * User: pc
 * Date: 2025/2/19
 * Time: 10:29
 */
namespace app\modules\setting\controllers\seller;

use app\common\controllers\AdminOptionsController;
use app\common\helper\CommonHelper;
use app\models\EtcSellerTiktokShop;
use app\models\QlTiktokShop;
use app\models\QlUserTiktokProductGoods;
use app\modules\setting\configs\seller\StoreConfig;
use app\modules\setting\models\seller\QlTiktokShopSearch;
use sookie\auth\models\Adminuser;
use Yii;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
use yii\base\Exception;
use sookie\excel\SaveExcel;
use sookie\db\DBHandler;

class StoreController extends AdminOptionsController
{
    /**
     * @return array
     * 列表数据
     */
    public function actionIndex()
    {
        try {
            $searchModel = new QlTiktokShopSearch();
            $dataProvider = $searchModel->search(Yii::$app->request->queryParams,['shop_name','shop_icon','id']);
            $data = $dataProvider->getModels();
            if($data){
                $data = array_map(function($item){
                    $item['shop_icon'] = "<img src='{$item['shop_icon']}' height=100 />";
                    return $item;
                },$data);
            }
            return ArrayHelper::merge(["data" =>$data, "pagination" => $dataProvider->getPagination()],StoreConfig::configIndex());
        }catch (\Throwable $e){
            return ["ok" => false,"msg" => $e->getMessage(),"code" => 401];
        }
    }

    /**
     * @param $id
     * @return array
     * 修改接口
     */
    public function actionUpdate($id)
    {
        try {
            $ql_shop = QlTiktokShop::find()->andWhere(['id'=>$id])->one();
            if(empty($ql_shop)){
                throw new Exception("店铺不存在");
            }
            if (Yii::$app->request->isPost){
                $code = Yii::$app->request->post("shop_code");
                if(empty($code)){
                    throw new Exception("请填写店铺编号");
                }
                if($code != $ql_shop->shop_code){
                    throw new Exception("店铺编号与关联店铺不一致，关联失败。");
                }
                $model = $this->findModel($code);
                if($model instanceof EtcSellerTiktokShop){
                    throw new Exception("店铺已被绑定，无法绑定。");
                }
                $info = Adminuser::getUserInfo();

                $model = new EtcSellerTiktokShop();
                $model->load($ql_shop->toArray(),'');
                $model->seller_id = $info->id;
                if(!$model->save()){
                    throw new Exception(CommonHelper::getFirstErrorStr($model->errors));
                }
                return ["msg" => "关联店铺成功"];
            }
            return StoreConfig::configUpdate($ql_shop);
        }catch (\Throwable $e){
            return ["ok" => false,"msg" => $e->getMessage(),"code" => 401];
        }
    }

    protected function findModel($code)
    {
        $info = Adminuser::getUserInfo();
        if (($model = EtcSellerTiktokShop::find()->andWhere(['shop_code'=>$code,'seller_id' => $info->id])->one()) !== null) {
            return $model;
        }
    }
}